import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../common/utils/toast.dart';
import '../main.dart';
import '../router/app_router.dart';
import '../router/router_utils.dart';

class TokenInterceptors extends InterceptorsWrapper {
  @override
  void onRequest(
      RequestOptions options, RequestInterceptorHandler handler) async {
    print('''
    请求方法:${options.method}
    请求baseUrl:${options.baseUrl}
    请求url:${options.path}
    请求头header:${options.headers.toString()}
    请求参数:${options.method == "GET" ? options.queryParameters.toString() : options.data.toString()}
    ''');
    return handler.next(options);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) async {
    try {
      print('==== Response data http====');
      Map<String, dynamic> responseData = response.data;
      print(response);
      EasyLoading.dismiss();
      if (responseData['code'] == 200) {
        //400参数错误
        return super.onResponse(response, handler);
      } else if (responseData['code'] == 401) {
        // RouterUtils.toRouter(globalContext()!, RouterValues.login,
        //   arguments: {
        //     "isBack": true,
        //   }
        // );
      } else {
        Toast.showToast(responseData["msg"]);
      }
    } catch (error) {
      print('Error parsing response data: $error');
    }

    return super.onResponse(response, handler);
  }

  @override
  void onError(DioError err, ErrorInterceptorHandler handler) {
    print(err);
  }
}
